- 
                Notifications
    You must be signed in to change notification settings 
- Fork 421
          Add electrum support to lightning-transaction-sync
          #2685
        
          New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
  
    Add electrum support to lightning-transaction-sync
  
  #2685
              Conversation
eadc4ba    to
    dfcc2f5      
    Compare
  
    | Codecov ReportAttention:  
 
 ❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@            Coverage Diff             @@
##             main    #2685      +/-   ##
==========================================
+ Coverage   88.50%   88.53%   +0.02%     
==========================================
  Files         113      113              
  Lines       89323    89334      +11     
  Branches    89323    89334      +11     
==========================================
+ Hits        79055    79090      +35     
+ Misses       7890     7875      -15     
+ Partials     2378     2369       -9     ☔ View full report in Codecov by Sentry. | 
c684ae0    to
    75ad902      
    Compare
  
    | Did some further cleanup, should be ready for review now. | 
75ad902    to
    e38abe0      
    Compare
  
    There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still have a bit to get through
e38abe0    to
    74cafd3      
    Compare
  
    74cafd3    to
    5ba1c45      
    Compare
  
    There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly looks good. Feel free to squash fixups. Will take a final pass after.
5ba1c45    to
    ce31cf2      
    Compare
  
    | Squashed previous fixups and addressed the pending comments. | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM after a couple minor comments
ce31cf2    to
    d9f6d41      
    Compare
  
    | Squashed fixups and included the following changes: diff --git a/lightning-transaction-sync/src/electrum.rs b/lightning-transaction-sync/src/electrum.rs
index a37d017fd..a002fc8e2 100644
--- a/lightning-transaction-sync/src/electrum.rs
+++ b/lightning-transaction-sync/src/electrum.rs
@@ -320,13 +320,13 @@ where
                                                        if let Some(history) = script_history.iter()
                                                                .filter(|h| h.tx_hash == txid).max_by_key(|x| x.height)
-                                                               {
-                                                                       let prob_conf_height = history.height;
-                                                                       let block_header = self.client.block_header(
-                                                                               prob_conf_height as usize)?;
-                                                                       if block_header.block_hash() == block_hash {
-                                                                               // Skip if the tx is still confirmed in the block in question.
-                                                                               continue;
-                                                                       }
+                                                       {
+                                                               let prob_conf_height = history.height;
+                                                               let block_header = self.client.block_header(
+                                                                       prob_conf_height as usize)?;
+                                                               if block_header.block_hash() == block_hash {
+                                                                       // Skip if the tx is still confirmed in the block in question.
+                                                                       continue;
                                                                }
+                                                       }
                                                }
                                        }
@@ -404,11 +404,6 @@ where
                for mut bytes in merkle_res.merkle {
                        bytes.reverse();
-                       let next_hash = Sha256d::from_slice(&bytes).map_err(|_| {
-                               log_error!(self.logger,
-                                       "Failed due to the server sending us bogus transaction data. This should not happen. Please verify server integrity."
-                               );
-                               InternalError::Failed
-                       })?;
-
+                       // unwrap() safety: `bytes` has len 32 so `from_slice` can never fail.
+                       let next_hash = Sha256d::from_slice(&bytes).unwrap();
                        let (left, right) = if index % 2 == 0 {
                                (cur, next_hash) | 
d9f6d41    to
    e14f83e      
    Compare
  
    | Moved logging and updating  > git diff-tree -U2 a48cf50b8 d609b5d37
diff --git a/lightning-transaction-sync/src/electrum.rs b/lightning-transaction-sync/src/electrum.rs
index 9d39ed87b..4dbd53609 100644
--- a/lightning-transaction-sync/src/electrum.rs
+++ b/lightning-transaction-sync/src/electrum.rs
@@ -113,7 +113,9 @@ where
                                                        // Double-check the tip hash. If it changed, a reorg happened since
                                                        // we started syncing and we need to restart last-minute.
-                                                       if self.check_has_chain_moved(&mut sync_state, &mut tip_header,
-                                                               &mut tip_height)?
+                                                       if self.check_update_tip(&mut tip_header, &mut tip_height)?
                                                        {
+                                                               log_debug!(self.logger,
+                                                                       "Encountered inconsistency during transaction sync, restarting.");
+                                                               sync_state.pending_sync = true;
                                                                continue;
                                                        }
@@ -145,7 +147,9 @@ where
                                                // Double-check the tip hash. If it changed, a reorg happened since
                                                // we started syncing and we need to restart last-minute.
-                                               if self.check_has_chain_moved(&mut sync_state, &mut tip_header,
-                                                       &mut tip_height)?
+                                               if self.check_update_tip(&mut tip_header, &mut tip_height)?
                                                {
+                                                       log_debug!(self.logger,
+                                                               "Encountered inconsistency during transaction sync, restarting.");
+                                                       sync_state.pending_sync = true;
                                                        continue;
                                                }
@@ -186,5 +190,7 @@ where
        }
-       fn check_has_chain_moved(&self, sync_state: &mut SyncState, cur_tip_header: &mut BlockHeader, cur_tip_height: &mut u32) -> Result<bool, InternalError> {
+       fn check_update_tip(&self, cur_tip_header: &mut BlockHeader, cur_tip_height: &mut u32)
+               -> Result<bool, InternalError>
+       {
                let check_notification = self.client.block_headers_subscribe()?;
                let check_tip_hash = check_notification.header.block_hash();
@@ -203,8 +209,4 @@ where
                        *cur_tip_header = check_notification.header;
                        *cur_tip_height = check_notification.height as u32;
-                       log_debug!(self.logger,
-                               "Encountered inconsistency during transaction sync, restarting.");
-                       sync_state.pending_sync = true;
-
                        Ok(true)
                } else { | 
85c0d77    to
    c8dca25      
    Compare
  
    | Now included two more commits that set  | 
c8dca25    to
    36ade54      
    Compare
  
    There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be good to go. I dunno if @jkczyz had any other thoughts, though.
We previously included the block hash, but it's also useful to include the height under which we expect the respective transaction to be confirmed.
We give some more information while reducing the log levels to make the logging less spammy. We also convert one safe-to-unwrap case from returning an error to unwrapping the value.
In particular, we now test `register_output` functionality, too.
36ade54    to
    c2e81fb      
    Compare
  
    | Rebased on main after #2740 landed. | 
Now that we upgraded `esplora-client` to 0.6 we can use `async-https-rustls` instead of manually overriding the `reqwest` dependency.
| Introduced a small follow-up commit to the rebase. As we've now upgraded to use  | 
Closes #2010.
This PR implements an
ElectrumSyncClientthat allows syncing LDK via theConfirm/Filterinterfaces based on BDK'selectrum-clientcrate.We furthermore improve the logging in
EsploraSyncClientandElectrumSyncClient, and extend our test coverage.Currently in draft as I want to do some further cleanup and experimentation.